package com.night.icm.service.base.service.comprehensive_test;

import com.night.icm.model.base.comprehensive_test.AcademicPointDTO;
import com.night.icm.persistence.base.entity.comprehensive_test.AcademicPoint;
import com.night.icm.persistence.base.entity.comprehensive_test.ComprehensiveScore;
import com.night.icm.persistence.base.repository.comprehensive_test.AcademicPointRepository;
import com.night.icm.persistence.business.entity.SystemFile;
import com.night.icm.persistence.business.reporsitory.SystemFileRepository;
import com.night.icm.service.base.mapper.comprehensive_test.AcademicPointMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * AcademicPointServiceImpl
 *
 * @author Liao ZhiYong
 * @date 2020/3/10
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class AcademicPointServiceImpl implements AcademicPointService {

    private final SystemFileRepository systemFileRepository;
    private final AcademicPointRepository academicPointRepository;
    private final AcademicPointMapper academicPointMapper;

    @Autowired
    public AcademicPointServiceImpl(SystemFileRepository systemFileRepository, AcademicPointRepository academicPointRepository, AcademicPointMapper academicPointMapper) {
        this.systemFileRepository = systemFileRepository;
        this.academicPointRepository = academicPointRepository;
        this.academicPointMapper = academicPointMapper;
    }

    /**
     * 新建学业分
     *
     * @param academicPointDTO   学业分
     * @param comprehensiveScore 综合分
     * @return 学业分
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AcademicPoint create(AcademicPointDTO academicPointDTO, ComprehensiveScore comprehensiveScore) {
        AcademicPoint academicPoint = this.academicPointMapper.toEntity(academicPointDTO);
        this.academicPointRepository.save(academicPoint);
        // 保存文体分的附件，并关联文体分附件
        List<String> fileIds = academicPointDTO.getFileIds();
        saveFile(academicPoint, fileIds);
        return academicPoint;
    }

    /**
     * 更新学业分及其附件
     *
     * @param academicPointDTO   学业分
     * @param comprehensiveScore 综合分
     * @return 学业分
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AcademicPoint update(AcademicPointDTO academicPointDTO, ComprehensiveScore comprehensiveScore) {
        // 更新学业分
        Optional<AcademicPoint> byId = this.academicPointRepository.findById(comprehensiveScore.getAcademicPoint().getId());
        AcademicPoint academicPoint = byId.get();
        this.academicPointMapper.updateEntity(academicPointDTO, academicPoint);
        // 删除原有附件
        academicPoint.setFiles(null);
        this.academicPointRepository.save(academicPoint);
        // 添加新的附件
        List<String> fileIds = academicPointDTO.getFileIds();
        saveFile(academicPoint, fileIds);
        this.academicPointRepository.save(academicPoint);
        return academicPoint;
    }

    /**
     * 保存文件
     *
     * @param academicPoint 对象
     * @param fileIds       文件id数组
     */
    private void saveFile(AcademicPoint academicPoint, List<String> fileIds) {
        if (null != fileIds && !fileIds.isEmpty()) {
            List<Long> ids = new ArrayList<>();
            fileIds.forEach(id -> ids.add(Long.parseLong(id)));
            List<SystemFile> files = this.systemFileRepository.findAllById(ids);
            academicPoint.setFiles(files);
        }
    }
}